<template>
  <div class="retail-outbound">
    <div class="page-header">
      <h1>零售出库管理</h1>
      <div class="header-actions">
        <button class="btn-primary" @click="showCreateModal = true">
          <i class="fas fa-plus"></i>
          新建出库单
        </button>
      </div>
    </div>

    <!-- 搜索和筛选 -->
    <div class="search-action-section">
      <div class="search-form">
        <div class="form-item">
          <label>订单编号</label>
          <input v-model="searchForm.orderCode" type="text" placeholder="请输入订单编号">
        </div>
        <div class="form-item">
          <label>客户类型</label>
          <select v-model="searchForm.customerType">
            <option value="">全部</option>
            <option value="VIP">VIP会员</option>
            <option value="Individual">散户</option>
          </select>
        </div>
        <div class="form-item">
          <label>订单状态</label>
          <select v-model="searchForm.status">
            <option value="">全部</option>
            <option value="Pending">待处理</option>
            <option value="Processing">处理中</option>
            <option value="Shipped">已发货</option>
            <option value="Delivered">已送达</option>
            <option value="Cancelled">已取消</option>
          </select>
        </div>
        <div class="form-item">
          <label>开始日期</label>
          <input v-model="searchForm.startDate" type="date">
        </div>
        <div class="form-item">
          <label>结束日期</label>
          <input v-model="searchForm.endDate" type="date">
        </div>
        <div class="form-buttons">
          <button class="btn btn-primary" @click="searchOrders">
            <i class="fas fa-search"></i>
            搜索
          </button>
          <button class="btn btn-secondary" @click="resetSearch">
            <i class="fas fa-refresh"></i>
            重置
          </button>
        </div>
      </div>
    </div>

    <!-- 订单列表和分页 -->
    <div class="table-pagination-section">
      <div class="table-container">
        <table>
          <thead>
            <tr>
              <th>订单编号</th>
              <th>客户信息</th>
              <th>客户类型</th>
              <th>订单金额</th>
              <th>支付方式</th>
              <th>订单状态</th>
              <th>创建时间</th>
              <th>操作</th>
            </tr>
          </thead>
          <tbody>
            <tr v-for="order in orders" :key="order.id">
              <td>
                <span class="order-code">{{ order.orderCode }}</span>
              </td>
              <td>
                <div class="customer-info">
                  <div class="customer-name">{{ order.customerName || '未填写' }}</div>
                  <div class="customer-phone">{{ order.customerPhone || '未填写' }}</div>
                </div>
              </td>
              <td>
                <span class="customer-type" :class="(order.customerType || '').toLowerCase()">
                  {{ (order.customerType || '') === 'VIP' ? 'VIP会员' : '散户' }}
                </span>
              </td>
              <td>
                <span class="amount">¥{{ (order.finalAmount || 0).toFixed(2) }}</span>
              </td>
              <td>
                <span class="payment-method">{{ getPaymentMethodText(order.paymentMethod) }}</span>
              </td>
              <td>
                <span class="status" :class="(order.orderStatus || order.status || 'pending').toLowerCase()">
                  {{ getStatusText(order.orderStatus || order.status) || '待处理' }}
                </span>
              </td>
              <td>
                <span class="create-time">{{ formatDate(order.orderDate) }}</span>
              </td>
              <td>
                <div class="actions">
                  <button class="btn-view" @click="viewOrder(order)">
                    <i class="fas fa-eye"></i>
                    查看
                  </button>
                  <button v-if="(order.orderStatus || order.status) === 'Pending'" class="btn-edit" @click="editOrder(order)">
                    <i class="fas fa-edit"></i>
                    编辑
                  </button>
                  <button v-if="(order.orderStatus || order.status) === 'Pending'" class="btn-process" @click="processOrder(order)">
                    <i class="fas fa-play"></i>
                    处理
                  </button>
                  <button v-if="(order.orderStatus || order.status) === 'Processing'" class="btn-ship" @click="shipOrder(order)">
                    <i class="fas fa-truck"></i>
                    发货
                  </button>
                </div>
              </td>
            </tr>
          </tbody>
        </table>
      </div>
      
      <!-- 分页 -->
      <div class="pagination">
        <button :disabled="currentPage === 1" @click="currentPage--">
          <i class="fas fa-chevron-left"></i>
        </button>
        <span class="page-info">
          第 {{ currentPage }} 页，共 {{ totalPages }} 页
        </span>
        <button :disabled="currentPage === totalPages" @click="currentPage++">
          <i class="fas fa-chevron-right"></i>
        </button>
      </div>
    </div>

    <!-- 创建出库单模态框 -->
    <div v-if="showCreateModal" class="modal-overlay" @click="showCreateModal = false">
      <div class="modal-content" @click.stop>
        <div class="modal-header">
          <h3>新建零售出库单</h3>
          <button class="modal-close" @click="showCreateModal = false">
            <i class="fas fa-times"></i>
          </button>
        </div>
        <div class="modal-body">
          <CreateOutboundForm @close="showCreateModal = false" @success="handleCreateSuccess" />
        </div>
      </div>
    </div>

    <!-- 查看订单模态框 -->
    <div v-if="showViewModal" class="modal-overlay" @click="showViewModal = false">
      <div class="modal-content large" @click.stop>
        <div class="modal-header">
          <h3>订单详情</h3>
          <button class="modal-close" @click="showViewModal = false">
            <i class="fas fa-times"></i>
          </button>
        </div>
        <div class="modal-body">
          <OrderDetailView :order="selectedOrder" />
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, onMounted, computed } from 'vue'
import CreateOutboundForm from './CreateForm.vue'
import OrderDetailView from './OrderDetail.vue'

export default {
  name: 'RetailOutboundIndex',
  components: {
    CreateOutboundForm,
    OrderDetailView
  },
  setup() {
    const showCreateModal = ref(false)
    const showViewModal = ref(false)
    const selectedOrder = ref(null)
    const currentPage = ref(1)
    const pageSize = ref(10)
    
    // 搜索表单
    const searchForm = ref({
      orderCode: '',
      customerType: '',
      status: '',
      startDate: '',
      endDate: ''
    })
    
    // 订单数据
    const orders = ref([])
    
    const totalPages = computed(() => Math.ceil(orders.value.length / pageSize.value))
    
    // 获取支付方式文本
    const getPaymentMethodText = (method) => {
      const methods = {
        'Cash': '现金',
        'Card': '银行卡',
        'Mixed': '混合支付',
        'Prepayment': '预付款',
        'Points': '积分'
      }
      return methods[method] || method
    }
    
    // 获取状态文本
    const getStatusText = (status) => {
      const statuses = {
        'Pending': '待处理',
        'Processing': '处理中',
        'Shipped': '已发货',
        'Delivered': '已送达',
        'Cancelled': '已取消'
      }
      return statuses[status] || status
    }
    
    // 格式化日期
    const formatDate = (date) => {
      return new Date(date).toLocaleDateString('zh-CN')
    }
    
    // 搜索订单
    const searchOrders = () => {
      // 实现搜索逻辑
      console.log('搜索订单:', searchForm.value)
    }
    
    // 重置搜索
    const resetSearch = () => {
      searchForm.value = {
        orderCode: '',
        customerType: '',
        status: '',
        startDate: '',
        endDate: ''
      }
      searchOrders()
    }
    
    // 查看订单
    const viewOrder = (order) => {
      selectedOrder.value = order
      showViewModal.value = true
    }
    
    // 编辑订单
    const editOrder = (order) => {
      console.log('编辑订单:', order)
    }
    
    // 处理订单
    const processOrder = (order) => {
      console.log('处理订单:', order)
    }
    
    // 发货
    const shipOrder = (order) => {
      console.log('发货:', order)
    }
    
    // 创建成功回调
    const handleCreateSuccess = () => {
      showCreateModal.value = false
      // 刷新列表
      loadOrders()
    }
    
    // 加载订单列表
    const loadOrders = async () => {
      try {
        const response = await fetch('/api/retail/orders', {
          method: 'GET',
          headers: {
            'Content-Type': 'application/json',
            'Authorization': `Bearer ${localStorage.getItem('token')}`
          }
        })
        
        if (response.ok) {
          const data = await response.json()
          orders.value = data.content || data.orders || []
        } else {
          console.error('加载订单列表失败:', response.statusText)
        }
      } catch (error) {
        console.error('加载订单列表失败:', error)
      }
    }
    
    onMounted(() => {
      loadOrders()
    })
    
    return {
      showCreateModal,
      showViewModal,
      selectedOrder,
      currentPage,
      pageSize,
      totalPages,
      searchForm,
      orders,
      getPaymentMethodText,
      getStatusText,
      formatDate,
      searchOrders,
      resetSearch,
      viewOrder,
      editOrder,
      processOrder,
      shipOrder,
      handleCreateSuccess
    }
  }
}
</script>

<style scoped>
.retail-outbound {
  padding: 20px;
  min-height: 100vh;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  position: relative;
}

.retail-outbound::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 100"><defs><pattern id="grain" width="100" height="100" patternUnits="userSpaceOnUse"><circle cx="25" cy="25" r="1" fill="rgba(255,255,255,0.1)"/><circle cx="75" cy="75" r="1" fill="rgba(255,255,255,0.1)"/><circle cx="50" cy="10" r="0.5" fill="rgba(255,255,255,0.1)"/><circle cx="10" cy="60" r="0.5" fill="rgba(255,255,255,0.1)"/><circle cx="90" cy="40" r="0.5" fill="rgba(255,255,255,0.1)"/></pattern></defs><rect width="100" height="100" fill="url(%23grain)"/></svg>');
  opacity: 0.3;
  pointer-events: none;
}

.page-header {
  margin-bottom: 30px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 25px 30px;
  border-radius: 16px;
  box-shadow: 0 8px 32px rgba(102, 126, 234, 0.3);
  position: relative;
  overflow: hidden;
}

.page-header::before {
  content: '';
  position: absolute;
  top: -50%;
  right: -50%;
  width: 200%;
  height: 200%;
  background: radial-gradient(circle, rgba(255,255,255,0.1) 0%, transparent 70%);
  animation: float 6s ease-in-out infinite;
}

@keyframes float {
  0%, 100% { transform: translateY(0px) rotate(0deg); }
  50% { transform: translateY(-20px) rotate(180deg); }
}

.page-header h1 {
  margin: 0 0 10px 0;
  font-size: 32px;
  color: white;
  font-weight: 700;
  text-shadow: 0 2px 4px rgba(0,0,0,0.3);
  position: relative;
  z-index: 1;
}

.header-actions {
  display: flex;
  gap: 12px;
  position: relative;
  z-index: 1;
}

.btn-primary {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 24px;
  background: rgba(255, 255, 255, 0.2);
  color: white;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-radius: 12px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  backdrop-filter: blur(10px);
}

.btn-primary:hover {
  background: rgba(255, 255, 255, 0.3);
  border-color: rgba(255, 255, 255, 0.5);
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.2);
}

.search-action-section {
  background: rgba(255, 255, 255, 0.95);
  padding: 25px 30px;
  border-radius: 16px;
  margin-bottom: 25px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
  backdrop-filter: blur(10px);
  position: relative;
  overflow: hidden;
}

.search-action-section::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: linear-gradient(90deg, #667eea 0%, #764ba2 50%, #f093fb 100%);
}

.search-section::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: linear-gradient(90deg, #667eea 0%, #764ba2 50%, #f093fb 100%);
}

.search-form {
  display: flex;
  gap: 20px;
  align-items: flex-end;
  flex-wrap: wrap;
}

.form-item {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.form-item label {
  font-weight: 500;
  color: #333;
  font-size: 14px;
}

.form-item input,
.form-item select {
  padding: 12px 16px;
  border: 2px solid #e1e5e9;
  border-radius: 12px;
  font-size: 14px;
  min-width: 180px;
  background: rgba(255, 255, 255, 0.9);
  transition: all 0.3s ease;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.form-item input:focus,
.form-item select:focus {
  outline: none;
  border-color: #667eea;
  box-shadow: 0 0 0 4px rgba(102, 126, 234, 0.1);
  transform: translateY(-2px);
  box-shadow: 0 4px 16px rgba(102, 126, 234, 0.2);
}

.form-item input:hover,
.form-item select:hover {
  border-color: #adb5bd;
  transform: translateY(-1px);
}

.form-buttons {
  display: flex;
  gap: 10px;
}

.btn {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 20px;
  border: none;
  border-radius: 12px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.btn-primary {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
}

.btn-primary:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(102, 126, 234, 0.4);
}

.btn-secondary {
  background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
  color: white;
}

.btn-secondary:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(240, 147, 251, 0.4);
}

.table-pagination-section {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 16px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
  backdrop-filter: blur(10px);
  position: relative;
  overflow: hidden;
  margin-bottom: 25px;
}

.table-pagination-section::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: linear-gradient(90deg, #4facfe 0%, #00f2fe 50%, #43e97b 100%);
}

.table-container {
  overflow-x: auto;
  padding: 20px 20px 0 20px;
}


.table-container table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}

.table-container th {
  background: linear-gradient(135deg, #f8fafc 0%, #f1f5f9 100%);
  padding: 16px 12px;
  text-align: left;
  font-weight: 700;
  color: #374151;
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  border-bottom: 2px solid #e2e8f0;
}

.table-container td {
  padding: 16px 12px;
  border-bottom: 1px solid rgba(225, 229, 233, 0.5);
  vertical-align: middle;
}

.table-container tbody tr {
  transition: all 0.3s ease;
}

.table-container tbody tr:hover {
  background: linear-gradient(135deg, rgba(102, 126, 234, 0.05) 0%, rgba(118, 75, 162, 0.05) 100%);
  transform: translateY(-2px);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
  border-radius: 8px;
}

.order-code {
  font-weight: 600;
  color: #667eea;
}

.customer-info {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.customer-name {
  font-weight: 500;
  color: #2d3748;
}

.customer-phone {
  font-size: 0.875rem;
  color: #718096;
}

.customer-type {
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 0.875rem;
  font-weight: 500;
}

.customer-type.vip {
  background: #fef5e7;
  color: #744210;
}

.customer-type.individual {
  background: #e6fffa;
  color: #234e52;
}

.amount {
  font-weight: 600;
  color: #2d3748;
}

.payment-method {
  font-size: 0.875rem;
  color: #4a5568;
}

.status {
  padding: 6px 12px;
  border-radius: 20px;
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  display: inline-block;
}

.status.pending {
  background: linear-gradient(135deg, #fef5e7 0%, #fed7aa 100%);
  color: #744210;
  border: 1px solid rgba(116, 66, 16, 0.3);
}

.status.processing {
  background: linear-gradient(135deg, #bee3f8 0%, #90cdf4 100%);
  color: #1a365d;
  border: 1px solid rgba(26, 54, 93, 0.3);
}

.status.shipped {
  background: linear-gradient(135deg, #c6f6d5 0%, #9ae6b4 100%);
  color: #22543d;
  border: 1px solid rgba(34, 84, 61, 0.3);
}

.status.delivered {
  background: linear-gradient(135deg, #e6fffa 0%, #b2f5ea 100%);
  color: #234e52;
  border: 1px solid rgba(35, 78, 82, 0.3);
}

.status.cancelled {
  background: linear-gradient(135deg, #fed7d7 0%, #feb2b2 100%);
  color: #742a2a;
  border: 1px solid rgba(116, 42, 42, 0.3);
}

.create-time {
  font-size: 0.875rem;
  color: #718096;
}

.actions {
  display: flex;
  gap: 8px;
}

.actions button {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 8px 16px;
  border: none;
  border-radius: 8px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.btn-view {
  background: linear-gradient(135deg, #e6fffa 0%, #b2f5ea 100%);
  color: #234e52;
  box-shadow: 0 2px 8px rgba(35, 78, 82, 0.2);
}

.btn-edit {
  background: linear-gradient(135deg, #fef5e7 0%, #fed7aa 100%);
  color: #744210;
  box-shadow: 0 2px 8px rgba(116, 66, 16, 0.2);
}

.btn-process {
  background: linear-gradient(135deg, #bee3f8 0%, #90cdf4 100%);
  color: #1a365d;
  box-shadow: 0 2px 8px rgba(26, 54, 93, 0.2);
}

.btn-ship {
  background: linear-gradient(135deg, #c6f6d5 0%, #9ae6b4 100%);
  color: #22543d;
  box-shadow: 0 2px 8px rgba(34, 84, 61, 0.2);
}

.actions button:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
}

.pagination {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 15px;
  padding: 20px;
  background: rgba(248, 249, 250, 0.8);
  border-top: 1px solid rgba(0, 0, 0, 0.05);
}

.pagination-info {
  color: #6b7280;
  font-size: 13px;
  font-weight: 500;
}

.pagination-controls {
  display: flex;
  gap: 8px;
}

.pagination button {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border: 2px solid #e5e7eb;
  background: white;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s ease;
  font-size: 13px;
  font-weight: 600;
}

.pagination button:hover:not(:disabled) {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border-color: #667eea;
  transform: translateY(-1px);
  box-shadow: 0 2px 8px rgba(102, 126, 234, 0.2);
}

.pagination button:disabled {
  opacity: 0.4;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  backdrop-filter: blur(4px);
}

.modal-content {
  background: white;
  border-radius: 16px;
  max-width: 600px;
  width: 90%;
  max-height: 85vh;
  overflow-y: auto;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
  border: 1px solid #e2e8f0;
}

.modal-content.large {
  max-width: 1000px;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 24px;
  border-bottom: 1px solid #e2e8f0;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  border-radius: 16px 16px 0 0;
}

.modal-header h3 {
  font-size: 18px;
  font-weight: 700;
  color: white;
  margin: 0;
}

.modal-close {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border: none;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 50%;
  cursor: pointer;
  transition: all 0.3s ease;
  font-weight: 600;
  color: white;
}

.modal-close:hover {
  background: rgba(255, 255, 255, 0.3);
  transform: scale(1.1);
}

.modal-body {
  padding: 24px;
  background: #fafbfc;
}

@media (max-width: 768px) {
  .search-form {
    flex-direction: column;
    align-items: stretch;
  }
  
  .form-group input,
  .form-group select {
    min-width: auto;
  }
  
  .page-header {
    flex-direction: column;
    gap: 16px;
    align-items: stretch;
  }
  
  .table-container {
    overflow-x: auto;
  }
  
  .actions {
    flex-direction: column;
  }
  
  .modal-content {
    width: 95%;
    margin: 10px;
  }
}
</style>
